/**
 * 
 */
package com.gosophia.conference.fileService;

import java.util.Calendar;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.entity.DocumentInfo;
import com.gosophia.conferenceManagement.common.service.ConferenceFileService;
import com.gosophia.contentRepo.domain.File;
import com.gosophia.contentRepo.domain.Resource;
import com.gosophia.contentRepo.service.FileSystemService;

/**
 * 功能：      
 * @author zhangdongsheng
 * @since  2011-3-25
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Service("conferenceFileService")
public class ConferenceFileServiceImpl implements ConferenceFileService {
    
    private final Logger logger  = Logger.getLogger(this.getClass());
    @Autowired
    @Qualifier("proxy")
    private FileSystemService fileSystemService;

    /* (non-Javadoc)
     * @see com.gosophia.conferenceManagement.common.service.ConferenceFileService#loadDocumentByPath(java.lang.String)
     */
    @Override
    public DocumentInfo loadDocumentByPath(String documentPath) {
          File file =(File) fileSystemService.findByPath(documentPath);
          if(file==null){
              logger.debug("未找到指定路径的文件");
          }else{
              logger.debug("file info=>name:"+file.getName()+" path:"+file.getPath());
          }
          return  convertFileToDocument(file);
       
    }


    /* (non-Javadoc)
     * @see com.gosophia.conferenceManagement.common.service.ConferenceFileService#saveDocumentData(com.gosophia.conferenceManagement.common.entity.DocumentInfo)
     */
    @Override
    public Document saveDocumentData(DocumentInfo documentInfo) {
        File file = new File();
        file.setName(documentInfo.getDocumentBasicInfo().getDocumentName());
        if(documentInfo.getDocumentBasicInfo().getDocumentPath() ==null){
            String filePath =generateFilePath();
            documentInfo.getDocumentBasicInfo().setDocumentPath(filePath);
            file.setPath(filePath);
        }else{
        	file.setPath(documentInfo.getDocumentBasicInfo().getDocumentPath());
        }
        file.setDescription(documentInfo.getDocumentBasicInfo().getDocumentNote());
        file.setCreationDate(Calendar.getInstance());
        Resource resource = new Resource();
        resource.setData(documentInfo.getDocumentContent());
        resource.setLastModified(Calendar.getInstance());
        resource.setMimeType("text/plain ");
        file.setResource(resource);
        logger.debug("文件的路径：" + documentInfo.getDocumentBasicInfo().getDocumentPath() );
        
        fileSystemService.save(file);//保存
        String version = fileSystemService.getLastedVersionName(documentInfo.getDocumentBasicInfo().getDocumentPath());
        logger.debug("saveDocumentData sucess");
        Document docInfo = documentInfo.getDocumentBasicInfo();
        docInfo.setDocumentVersion(version);
        logger.debug("保存文档成功信息:"+ docInfo.toString());
        return docInfo;
    }
    
    private String generateFilePath(){
        return "/attechments/"+UUID.randomUUID().toString();
    }
 
    
    private DocumentInfo convertFileToDocument(File file){
        DocumentInfo info = new DocumentInfo();
        Document documentBasicInfo = new Document(); 
        documentBasicInfo.setDocumentName(file.getName());
        documentBasicInfo.setDocumentPath(file.getPath());
        documentBasicInfo.setDocumentNote(file.getDescription());
        info.setDocumentBasicInfo(documentBasicInfo);
        info.setDocumentContent(file.getResource().getData());
        return info ;
    }

    /* (non-Javadoc)
     * @see com.gosophia.conferenceManagement.common.service.ConferenceFileService#loadDocument(com.gosophia.conferenceManagement.common.entity.Document)
     */
    @Override
    public DocumentInfo loadDocument(Document document) {
   
        return loadDocumentByPathAndVersion(document.getDocumentPath(),document.getDocumentVersion());
    }

    /*  
     * @see com.gosophia.conferenceManagement.common.service.ConferenceFileService#loadDocumentByPathAndVersion(java.lang.String, java.lang.String)
     */
    @Override
    public DocumentInfo loadDocumentByPathAndVersion(String path, String version) {
        File file =   fileSystemService.getFileByVersion(path, version);
        DocumentInfo doc = convertFileToDocument(file);
        doc.getDocumentBasicInfo().setDocumentVersion(version);
        return doc;
    }

}
